import os

import pytest

from utils.log_util import logger
from api.user_api import login_user
from utils.read import base_data


@pytest.fixture(scope='function', autouse=True)
def func():
    logger.info("开始执行测试用例")
    yield
    logger.info("测试用例执行结束")


def get_data():
    return base_data.read_data()


@pytest.fixture(scope='session', autouse=True)
def login_fixture():
    data = get_data()['login_user1']
    # mobile = data['username']
    # password = data['password']
    # 登录接口
    if "token" not in os.environ:
        result = login_user(data)
        os.environ['token'] = result.body['token']
        return result.body['token']
    else:
        return os.environ['token']


@pytest.fixture(scope='session')
def login_token():
    data = get_data()['login_user1']
    # mobile = data['username']
    # password = data['password']
    # 登录接口
    result = login_user(data)
    headers = {
        "Authorization": "JWT " + result.body['token']
    }
    return headers
